package de.tuberlin.dima.dkey.examples;

import java.io.File;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import de.tuberlin.dima.dkey.client.Client;
import de.tuberlin.dima.dkey.client.GTree;
import de.tuberlin.dima.dkey.util.GNode;
import de.tuberlin.dima.dkey.util.JsonTreeHelper;

public class SimpleQueryFunction {
	
	public static void main(String[] args) {
		
		//Set up the logging
	    BasicConfigurator.configure();
		Logger.getRootLogger().setLevel(Level.ERROR);
		
		String queryString = "{\"predicate\":{\"lemma\":\"met\",\"arguments\":[{\"text\":\"Regex(Wed*)\"},{\"text\":\"in Brussels\"}]}}";
		
		JsonTreeHelper creator = new JsonTreeHelper();
		GTree query;
		try{
			query = creator.createTree(queryString);
		}catch (com.google.gson.stream.MalformedJsonException e){
			System.err.println("The input Json is Malformed\n" + e.getLocalizedMessage());
			return;
		}
				
		if (query.isValid()){
			//Send it to the server
			try {
				Client client = new Client("localhost", 8080);
				
				long time = System.currentTimeMillis();

				client.query(query);
				
				BlockingQueue<String> answer = client.getReturnQueue();
				System.out.println(answer.take());

				System.out.println("It took " + (System.currentTimeMillis() - time) + " milliseconds");
				
				client.end();


			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}else{
			System.err.print("Tree is not valid");
		}	
	}
}
